package Future;

import java.util.Arrays;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import javax.swing.JOptionPane;

public class GetThreadRunTime implements Runnable {

	static int count = 0;

	public static void main(String[] args) {
		ThreadPoolExecutor ThreadPools = new ThreadPoolExecutor(1, 1, 1,
				TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
		GetThreadRunTime d = new GetThreadRunTime();
		// Thread beginTest = new Thread(d);
		ThreadPools.execute(d);
		ThreadPools.shutdown(); // 关闭后不能加入新线程，队列中的线程并非依次执行完
		int temp = 0;
		while ((temp = ThreadPools.getPoolSize()) != 0) {
			try {
				Thread.sleep(1000);
				System.out.println("程序运行了 " + count + "秒");
				if (count >= 4) {
                        break;
				}
				count++;
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}

	}

	public static double generate(int max) {// 质数过滤算法
		long start = System.currentTimeMillis();
		boolean sieve[] = new boolean[max];
		Arrays.fill(sieve, true);
		sieve[0] = false;
		sieve[1] = false;
		for (int i = 2; i < Math.sqrt(max); i++) {
			if (sieve[i]) {
				for (int j = 2 * i; j < sieve.length; j += i) {
					sieve[j] = false;
				}
			}
		}
		return (System.currentTimeMillis() - start);
	}

	public void run() {
		try {
			Thread.sleep(10000);
			// int n = JOptionPane.showConfirmDialog(null,"good","确认对话框",
			// JOptionPane.YES_NO_OPTION);
			// if(n==JOptionPane.YES_OPTION){
			//
			// }
			// else if(n==JOptionPane.NO_OPTION){
			//
			// }
			System.out.println("结束end");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
